<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="title">{{$t('approval.title1')}}</div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 业务ID -->
          <div class="searchItem">
            <span>{{$t('approval.orderId')}}</span>
            <el-input v-model="listQuery.orderId" :placeholder="$t('rule.orderId')" size="small"></el-input>
          </div>
          <!-- 客户姓名 -->
          <div class="searchItem">
            <span>{{$t('approval.realName')}}</span>
            <el-input v-model="listQuery.realName" :placeholder="$t('rule.realName')" size="small"></el-input>
          </div>
          <!-- 手机号 -->
          <div class="searchItem">
            <span>{{$t('approval.phoneNo')}}</span>
            <el-input v-model="listQuery.phoneNo" :placeholder="$t('rule.phoneNo')" size="small"></el-input>
          </div>
          <!-- 审批结果 -->
          <div class="searchItem">
            <span>{{$t('approval.result')}}</span>
            <el-select v-model="listQuery.approvalResult" size="small">
              <el-option
                v-for="item in result"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <!-- 处理状态 -->
          <div class="searchItem">
            <span>{{$t('approval.status')}}</span>
            <el-select v-model="listQuery.delStatus" size="small">
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <!-- 是否资料退回 -->
          <div class="searchItem">
            <span>{{$t('approval.isDataReturn')}}</span>
            <el-select v-model="listQuery.dataReturn" size="small">
              <el-option
                v-for="item in returnArr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <!-- 审批时间 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('approval.checkDate')}}</span>
            <el-date-picker
              size="small"
              v-model="apprDate"
              type="daterange"
              :range-separator="$t('btn.go')"
              :start-placeholder="$t('rule.beginDate')"
              :end-placeholder="$t('rule.endDate')"
              value-format="yyyy-MM-dd"
              @change="dateChangeCheckTime"
            ></el-date-picker>
          </div>
          <!-- 申请时间 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('approval.time')}}</span>
            <el-date-picker
              size="small"
              v-model="applyDate"
              type="daterange"
              :range-separator="$t('btn.go')"
              :start-placeholder="$t('rule.beginDate')"
              :end-placeholder="$t('rule.endDate')"
              value-format="yyyy-MM-dd"
              @change="dateChangeApplyDate"
            ></el-date-picker>
          </div>
        </div>
        <div class="menu-title-btn">
          <button type="button" @click="search">{{$t('btn.query')}}</button>
          <button type="button" @click="reset">{{$t('btn.reset')}}</button>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('approval.title2')}}</div>
      <div class="menu-list">
        <div class="left">
          <!-- 审批 -->
          <button type="button" :disabled="isDisable2" @click="approvalFn">{{$t('approval.appr')}}</button>
          <!-- 领取任务 -->
          <button type="button" @click="receive">{{$t('approval.task')}}</button>
        </div>
        <div class="statistics">
          <!-- 被退回数量 -->
          <el-badge :value="statistics.datumBack" class="item">
            <el-button size="small">{{$t('approval.QuantityReturned')}}</el-button>
          </el-badge>
          <!-- 我的完成数量 -->
          <el-badge :value="statistics.userCheck" class="item">
            <el-button size="small">{{$t('approval.userCheck')}}</el-button>
          </el-badge>
          <!-- 待审核数量 -->
          <el-badge :value="statistics.notGet" class="item">
            <el-button size="small">{{$t('approval.notGet')}}</el-button>
          </el-badge>
        </div>
      </div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        border
        stripe
        size="mini"
      >
        <el-table-column header-align="center" align="center" type="selection" width="55"></el-table-column>
        <!-- 业务ID -->
        <el-table-column  align="center" header-align="center"  prop="orderId" :label="$t('approval.orderId')" width="140"></el-table-column>
        <!-- 客户姓名 -->
        <el-table-column align="center" header-align="center" prop="realName" :label="$t('approval.realName')" width="180"></el-table-column>
        <!-- 手机号 -->
        <el-table-column align="center" header-align="center" prop="appUserName" :label="$t('approval.phoneNo')" width="140"></el-table-column>
        <!-- 产品名称 -->
        <el-table-column align="center" header-align="center" prop="proName" :label="$t('approval.proName')" width="140"></el-table-column>
        <!-- 金额 -->
        <el-table-column align="center" header-align="center" prop="applyAmt" :label="$t('approval.applyAmt')" width="120"></el-table-column>
        <!-- 申请时间 -->
        <el-table-column align="center" header-align="center" prop="crtTime" :label="$t('approval.time')" width="200"></el-table-column>
        <!-- 审批时间 -->
        <el-table-column align="center" header-align="center" prop="checkDate" :label="$t('approval.checkDate')" width="200"></el-table-column>
        <!-- 业务渠道 -->
        <el-table-column align="center" header-align="center" prop="appShortName" :label="$t('approval.channel')" width="120"></el-table-column>
        <!-- 处理状态 -->
        <el-table-column align="center" header-align="center" prop="status" :label="$t('approval.status')" width="130">
          <template slot-scope="scope">
            <span v-if="scope.row.status=== 1">{{$t('approval.audit')}}</span>
            <span v-if="scope.row.status=== 2">{{$t('approval.credit')}}</span>
            <span v-if="scope.row.status=== 3">{{$t('approval.repayments')}}</span>
            <span v-if="scope.row.status=== 4">{{$t('approval.refused')}}</span>
            <span v-if="scope.row.status=== 5">{{$t('approval.clear')}}</span>
          </template>
        </el-table-column>
        <!-- 资料退回原因 -->
        <el-table-column align="center" header-align="center" prop="orderBackReason" :label="$t('approval.orderBackReason')" width="200"></el-table-column>
        <!-- 审批结果 -->
        <el-table-column align="center" header-align="center" prop="checkUserStatus" :label="$t('approval.result')" width="130">
          <template slot-scope="scope">
            <span v-if="scope.row.checkUserStatus=== 0">{{$t('approval.noappr')}}</span>
            <span v-else-if="scope.row.checkUserStatus=== ''">{{$t('approval.noappr')}}</span>
            <span v-else-if="scope.row.checkUserStatus=== null">{{$t('approval.noappr')}}</span>
            <span v-else>{{$t('approval.handle')}}</span>
          </template>
        </el-table-column>
        <!-- 审批意见 -->
        <el-table-column
          prop="checkOpinion" align="center" header-align="center" 
          :label="$t('approval.checkOpinion')"
          width="300"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog :title="$t('approval.task')" :visible.sync="dialogFormVisible" width="40%"
  :before-close="handleClose">
      <div style="width: 100%; display: flex; justify-content: center;">
        <el-form style="width: 100%;" :model="form" :rules="rules" ref="form" label-width="100px">
          <!-- 任务类型 -->
          <el-form-item :label="$t('approval.taskType')" prop="taskType">
            <el-radio-group v-model="form.taskType">
              <el-radio
              v-for='item in taskTypeArr'
              :key='item.pType'
              :label='item.pType'>
              <span v-if="item.pType === '0'">
              {{$t('rule.all') + '(' + item.count + $t('approval.piece') + ')'}}</span>
              <span v-if="item.pType !== '0'">
              {{item.pType + $t('processing.day') + '(' + item.count + $t('approval.piece') + ')'}}</span>
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
        <div slot="footer" class="dialog-footer" style="text-align: center;">
          <el-button type="primary" @click="submitForm('form')">{{$t('btn.confirm')}}</el-button>
          <el-button @click="resetForm('form')">{{$t('btn.cancel')}}</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getTask, page, approvalData, selectPtermCount } from "api/trust/firstApproval/index";
export default {
  data() {
    return {
      apprDate: [],
      applyDate: [],
      taskTypeArr: [],
      dialogFormVisible: false,
      form: {
        taskType: ''
      },
      rules: {
        taskType: [
          {
            required: true,
            message: this.$t("approval.ruleTaskType"),
            trigger: "change"
          }
        ]
      },
      statistics: {},
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 5,
        approvalFlag: "1",
        orderId: "",
        realName: "",
        phoneNo: "",
        approvalResult: 4,
        delStatus: 1,
        beginDate: "",
        endDate: "",
        checkBeginDate: "", //审批时间
        checkEndDate: "",
        dataReturn: ""
      },
      total: null,
      tableData: [],
      multipleSelection: [],
      result: [
        {
          value: "",
          label: this.$t("rule.all")
        },
        {
          value: 1,
          label: this.$t("approval.adopt")
        },
        {
          value: 2,
          label: this.$t("approval.refuse")
        },
        // {
        //   value: 3,
        //   label: this.$t('approval.derate')
        // },
        {
          value: 4,
          label: this.$t("approval.audit")
        }
      ],
      status: [
        {
          value: "",
          label: this.$t("rule.all")
        },
        {
          value: 1,
          label: this.$t("approval.signin")
        },
        {
          value: 2,
          label: this.$t("approval.handle")
        }
      ],
      returnArr: [
        {
          value: 1,
          label: this.$t("approvalDetail.yes")
        },
        {
          value: 2,
          label: this.$t("approvalDetail.no")
        }
      ],
      isDisable2: true
    };
  },
  computed: {
    ...mapGetters(["elements", "language", "reviewApprPage"])
  },
  created() {
    //统计
    this.countFn();
    if (this.reviewApprPage.has(this.$route.path)) {
      this.listQuery = this.reviewApprPage.get(this.$route.path);
    }
    this.getList();
  },
  methods: {
    //统计
    countFn() {
      let obj = {};
      obj.type = "1";
      approvalData(obj).then(res => {
        this.statistics = res.data;
      });
    },
    // 审批时间
    dateChangeCheckTime(val) {
      if (val === undefined || val === null || val === '') {
        this.listQuery.checkBeginDate = '';
        this.listQuery.checkEndDate = '';
      } else {
        this.listQuery.checkBeginDate = val[0];
        this.listQuery.checkEndDate = val[1];
      }
    },
    // 申请时间
    dateChangeApplyDate(val) {
      if (val === undefined || val === null || val === '') {
        this.listQuery.beginDate = '';
        this.listQuery.endDate = '';
      } else {
        this.listQuery.beginDate = val[0];
        this.listQuery.endDate = val[1];
      }
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.orderId = "";
      this.listQuery.realName = "";
      this.listQuery.phoneNo = "";
      this.listQuery.approvalResult = 4;
      this.listQuery.delStatus = 1;
      this.applyDate = [];
      this.apprDate = [];
      this.listQuery.checkBeginDate = '';
      this.listQuery.checkEndDate = '';
      this.listQuery.beginDate = '';
      this.listQuery.endDate = '';
      this.listQuery.dataReturn = "";
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveReviewApprPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      if (
        (this.tableData.length > 0 && this.listQuery.dataReturn === "") ||
        this.listQuery.dataReturn === 2
      ) {
        this.isDisable2 = false;
      } else {
        this.isDisable2 = true;
      }
      page(this.listQuery).then(response => {
        response.data.rows.forEach(item => {
          let re = /(?=(?!\b)(\d{3})+$)/g;
          let str = String(item.applyAmt);
          item.applyAmt = str.replace(re, ",");
        });
        if (response.status === 200) {
          this.listLoading = false;
          this.tableData = response.data.rows;
          this.total = Number(response.data.total);
        }
      });
    },
    handleClose(done) {
      done();
      this.$refs["form"].resetFields();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          let obj = {};
          obj.type = '1';
          // obj.detailType = "0";
          // if (this.form.taskType === this.$t('rule.all')) {
          //   obj.pTerm = '0';
          // } else {
          //   obj.pTerm = this.form.taskType;
          // }
          getTask(obj).then(response => {
            if (response.status === 200) {
              this.$message({
                showClose: true,
                message: response.message,
                type: "success"
              });
              this.getList();
              this.countFn();
            } else {
              this.$message({
                showClose: true,
                message: response.message,
                type: "warning"
              });
            }
            // if (response.data.apply === 0) {
            //   this.$message({
            //     showClose: true,
            //     message: this.$t("approval.error"),
            //     type: "warning"
            //   });
            //   this.getList();
            //   this.countFn();
            // } else if (response.data.apply === 1) {
            //   this.$message({
            //     showClose: true,
            //     message: this.$t("approval.success"),
            //     type: "success"
            //   });
            //   this.getList();
            //   this.countFn();
            // } else if (response.data.apply === 2) {
            //   this.$message({
            //     showClose: true,
            //     message: this.$t("approval.most"),
            //     type: "warning"
            //   });
            //   this.getList();
            //   this.countFn();
            // } else if (response.data === "") {
            //   this.$message({
            //     showClose: true,
            //     message: response.message,
            //     type: "warning"
            //   });
            //   this.getList();
            //   this.countFn();
            // }
            if (this.tableData.length > 0) {
              this.isDisable2 = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    // 领取任务
    receive() {
      this.form.taskType = '';
      this.taskTypeArr = [];
      let obj = {};
      obj.type = '1';
      selectPtermCount(obj)
      .then(res => {
        if (res.status === 200) {
          res.data.forEach(item => {
            if (item.pType === '0') {
              this.taskTypeArr.push(item)
            }
          })
          this.dialogFormVisible = true;
        }
      })
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
      if (this.multipleSelection.length !== 0) {
        if (
          (this.multipleSelection[0].checkUserStatus === 0 ||
            this.multipleSelection[0].checkUserStatus === null ||
            this.multipleSelection[0].checkUserStatus === "") &&
          (this.multipleSelection[0].attr8 === "" ||
            this.multipleSelection[0].attr8 === "2")
        ) {
          this.isDisable2 = false; //不禁用
        } else {
          this.isDisable2 = true; //禁用
        }
      }
    },
    //审批
    approvalFn() {
      if (this.multipleSelection.length == 1) {
        this.$store.dispatch("setReviewApprDetails", this.multipleSelection[0]);
        this.$router.push({
          path: "/trust/reviewApprDetails",
          name: "复审详情 - Beta"
        });
      } else if (this.multipleSelection.length <= 0) {
        this.$message({
          showClose: true,
          message: this.$t("approval.msg1"),
          type: "warning"
        });
      } else {
        this.$message({
          showClose: true,
          message: this.$t("approval.msg2"),
          type: "warning"
        });
      }
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 220px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  .left {
    button {
      padding: 0 20px;
      height: 28px;
      background: #20a0ff;
      color: #fff;
      outline: none;
      border-width: 0;
      border-radius: 4px;
      font-size: 12px;
      margin-left: 10px;
      &:nth-child(1) {
        margin-left: 0;
      }
    }
    button[disabled] {
      padding: 0 20px;
      height: 28px;
      background: #c1e0ff;
      color: #fafbff;
      outline: none;
      border-width: 0;
      border-radius: 4px;
      font-size: 12px;
    }
  }
}
.el-date-editor.el-input {
  width: 193px;
}
.menu-list {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.statistics {
  div {
    margin-right: 40px;
    font-size: 16px;
  }
}
</style>